<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SportsTest;
use App\Models\Sport;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class SportsTestController extends AdminController
{

    public function index(Content $content)
    {
        return $content->title('体测')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SportsTest(), function (Grid $grid) {
            $grid->model()->orderByDesc('created_at');
            $grid->column('id')->sortable();
            $grid->fixColumns(2, -1);
            $grid->column('user_id')->display(function ($value){
              return User::where('id',$value)->value('user_name') ?? '';
            });
            $grid->column('age')->display(function ($value){
                return $value.'岁' ?? '';
            });
            $grid->column('height')->display(function ($value){
                return $value.'cm' ?? '';
            });
            $grid->column('weight')->display(function ($value){
                return $value.'KG' ?? '';
            });
            $grid->column('long_jump','跳远')->display(function ($value){
                return $value.'m' ?? '';
            });
            $grid->column('sargent_jump','摸高')->display(function ($value){
                return $value.'m' ?? '';
            });
            $grid->column('arm_spread')->display(function ($value){
                return $value.'cm' ?? '';
            });
            $grid->column('finger_span')->display(function ($value){
                return $value.'cm' ?? '';
            });
            $grid->column('sitting_position','坐位体前屈')->display(function ($value){
                return $value.'cm' ?? '';
            });
            $grid->column('jump')->display(function ($value){
                return $value.'个' ?? '';
            });
            $grid->column('sensitive','禁区灵敏测试')->display(function ($value){
                return $value.'s' ?? '';
            });
            $grid->column('toss_ball')->display(function ($value){
                return $value.'m' ?? '';
            });
            $grid->column('vertical_jump')->display(function ($value){
                return $value.'cm' ?? '';
            });
            $grid->column('sprint','3/4冲刺')->display(function ($value){
                return $value.'s' ?? '';
            });
            $grid->column('back_forth','15米往返跑')->display(function ($value){
                return $value.'圈' ?? '';
            });
            $grid->column('legs','下肢力量')->display(function ($value){
                return $value.'cm' ?? '';
            });
            $grid->column('upper_limb','上肢力量')->display(function ($value){
                return $value.'m' ?? '';
            });
            $grid->column('move_flex','移动灵活性')->display(function ($value){
                return $value.'s' ?? '';
            });
            $grid->column('bounce_sen','弹跳灵敏性')->display(function ($value){
                return $value.'个' ?? '';
            });
            $grid->column('flex','柔韧性')->display(function ($value){
                return $value.'cm' ?? '';
            });
            $grid->column('oxygen','有氧能力')->display(function ($value){
                return $value.'圈' ?? '';
            });
            $grid->column('spend','速度')->display(function ($value){
                return $value.'s' ?? '';
            });
            $grid->column('point','综合评分');
            $grid->column('super','优秀占比');
            $grid->column('good','良好占比');
            $grid->column('pass','及格占比');
            $grid->column('poor','差占比');

            $grid->column('BMI');

            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->where('user_id', function ($query) {
                    $query->whereHas('userSport', function ($query) {
                        $query->where('user_name', 'like', "%{$this->input}%");
                    });
                }, '用户名称')->width(3);
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                $actions->disableDelete();
//                $actions->disableEdit();
//                $actions->disableQuickEidt();
                $actions->disableView();
            });
            //禁用新增按钮
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SportsTest(), function (Show $show) {
            $show->field('id');
            $show->field('height');
            $show->field('weight');
            $show->field('BMI');
            $show->field('arm_spread');
            $show->field('finger_span');
            $show->field('sitting_position');
            $show->field('jump');
            $show->field('sensitive');
            $show->field('toss_ball');
            $show->field('vertical_jump');
            $show->field('age');
            $show->field('user_id');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SportsTest(), function (Form $form) {
            $form->display('id');
            $form->select('user_id')->options('user/player');
            $form->text('age')->help('默认单位岁');
            $form->text('height')->help('默认单位cm');
            $form->text('weight')->help('默认单位KG');
            $form->text('long_jump','跳远')->help('默认单位m');
            $form->text('sargent_jump','摸高')->help('默认单位m');
            $form->text('arm_spread')->help('默认单位cm');
            $form->text('finger_span')->help('默认单位cm');
            $form->text('sitting_position','坐位体前屈')->help('默认单位cm');
            $form->text('jump')->help('默认单位个');
            $form->text('sensitive','禁区灵敏测试')->help('默认单位s');
            $form->text('toss_ball')->help('默认单位m');
            $form->text('vertical_jump')->help('默认单位cm');
            $form->text('sprint','3/4冲刺')->help('默认单位s');
            $form->text('back_forth','15米往返跑')->help('默认单位圈');
            $form->text('legs','下肢力量')->help('默认单位cm');
            $form->text('upper_limb','上肢力量')->help('默认单位m');
            $form->text('move_flex','移动灵活性')->help('默认单位s');
            $form->text('bounce_sen','弹跳灵敏性')->help('默认单位个');
            $form->text('flex','柔韧性')->help('默认单位cm');
            $form->text('oxygen','有氧能力')->help('默认单位圈');
            $form->text('spend','速度')->help('默认单位s');
            $form->text('BMI','BMI');
            $form->text('point','综合评分');
            $form->text('super','优秀占比');
            $form->text('good','良好占比');
            $form->text('pass','及格占比');
            $form->text('poor','差占比');
            $form->display('created_at');
            $form->display('updated_at');
            $form->saved(function (Form $form){
                $uid =  $form->user_id;
                if ($form->isCreating()){
                      $res = Sport::where('user_id',$uid)->orderby('created_at','desc')->limit(2)->get();
                      $len = count($res);
                      if ($len == 2){
                          $res = Sport::where('id',$res[1]->id)->update(['target'=>0]);
                      }
                      if ($res) {
                          return $form->redirect('SportsTest', '保存成功');
                      } else {
                          //修改删除新建的
                          Sport::where('id',$res[0]->id)->delete();
                          return $form->error('数据保存失败');
                      }
                }
            });
        });
    }
}
